<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='文章管理',active='article'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-4">
                        <h4 class="page-title">文章管理</h4>
                    </div>
                    <div class="col-sm-3">
                    </div>
                    <div class="col-sm-2">
                        <h4 class="page-title dropdown">
                            <a href="/admin/article">
                                全部文章</a>
                            </h4>
                    </div>
                    <div class="col-sm-1 dropdown" role="presentation">
                        <h4 class="page-title">
                         <a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
                            分类 <span class="caret"></span>
                         </a>
                         <ul class="dropdown-menu">
                             <th:block th:each="cat : ${categoriesList}">
                             <li><a href="#" onclick="tagSubmit('cate',this)"><th:block th:text="${cat}"/></a></li>


                             </th:block>
                         </ul>
                        </h4>
                    </div>
                    <div class="col-sm-1">
                        <h4 class="page-title dropdown">
                            <a href="javascript:void(0);" class="" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                                归档</a>
                        </h4>
                    </div>
                    <div class="col-sm-1">
                        <h4 class="page-title dropdown">
                            <a href="#" data-toggle="modal" data-target="#myModal">
                                标签</a>
                            <!-- 标签Modal弹窗 -->
                            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                                <div class="modal-dialog modal-lg" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                            <h4 class="modal-title" id="myModalLabel">共<th:block th:text="${tagsList!=null?tagsList.size():0}"/>个标签 </h4>
                                        </div>
                                        <div class="modal-body">
                                            <th:block th:each="tag : ${tagsList}">
                                                <button onclick="tagSubmit('tags',this);" type="button" class="btn btn-success" style="margin-left: 5px;margin-top: 10px;"><th:block th:text="${tag}"/></button>

                                            </th:block>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                            <a href="/admin/category" class="btn btn-primary">管理我的标签</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </h4>
                    </div>




                    <div class="col-md-12">
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th width="35%">文章标题</th>
                                <th width="15%">发布时间</th>
                                <th>浏览量</th>
                                <th>所属分类</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="article : ${articles.list}">
                                <tr th:id="${article.id}">
                                    <td>
                                        <a th:href="@{'/article/'+${article.id}}" th:text="${article.title}" target="_blank">

                                        </a>
                                        <a th:if="${article.secret}" th:href="@{'/article/'+${article.id}}" target="_blank" title="私密文章">
                                            &nbsp;<i class="fa fa-lock fa-lg" ></i>
                                        </a>

                                    </td>
                                    <td><th:block th:text="${commons.dateFormat(article.created)}"/></td>
                                    <td><th:block th:text="${article.hits}"/></td>
                                    <td><th:block th:text="${article.categories}"/></td>
                                    <td>
                                        <a th:href="@{'/admin/article/'+${article.id}}"
                                           class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                           <i class="fa fa-edit"></i> <span>编辑</span></a>
                                        <a href="javascript:void(0)" th:onclick="'delArticle('+${article.id}+');'"
                                           class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                            <i class="fa fa-trash-o"></i> <span>删除</span></a>
                                        <a class="btn btn-warning btn-sm waves-effect waves-light m-b-5" th:href="@{'/article/'+${article.id}}">
                                            <i class="fa fa-rocket"></i> <span>预览</span></a>
                                    </td>
                                </tr>
                            </th:block>

                            </tbody>
                        </table>
                        <div th:replace="comm/paging :: pageAdminNav(${articles},${cate})"></div>
                    </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
<script type="text/javascript">
    //标签分类
    $('#myModal').on('shown.bs.modal', function () {
        $('#myInput').focus()
    });

    //地址栏信息修改，分页修改
    function tagsSubmit(ta){

        ta=""+ta;
        var s = window.location.href;

        s = set(s,'page',ta);

        window.location.href=s;
    }
    //分类修改
    function tagSubmit(key,ev){
        var ta = $(ev).text();
        var value="";
        for(var i=0;i<ta.length;i++){
            if(ta[i]==' ')continue;
            value+=ta[i];
        }
        var s = window.location.href;
        s = set(s,key,value);
        s = set(s,"page","");
        window.location.href=s;
    }
    //s2在s1中的位置
    function stains(s1,s2){
        var begin=0;while(begin<s1.length&&s1[begin]!='?')begin++;
        if(begin>=s1.length)return 0;
        for(var i=begin;i<s1.length;i++){
            var j1=i,j2=0;
            while (j1<s1.length&&j2<s2.length&&s1[j1]==s2[j2]){
                j1++;j2++;
            }
            if(j2>=s2.length&&(s1[j1]=='='))return j1;
        }
        return -1;
    }
    function set(href,key,value){
        var id = stains(href,key);
        if(id==0){
            return href+"?"+key+"="+value;
        }
        else if(id==-1){
            return href+"&"+key+"="+value;
        }else{
            var res="";
            for(var i=0;i<=id;i++){
                res+=href[i];
            }
            while(id<href.length&&href[id]!='&')id++;
            res+=value;
            for(var i=id;i<href.length;i++)res+=href[i];
            return res;
        }

    }
    /*<![CDATA[*/
    function delArticle(id) {
        // 获取<meta>标签中封装的_csrf信息
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        if(confirm('确定删除该文章吗?')){
            $.ajax({
                type:'post',
                url : '/admin/article/delete',
                data: {id:id},
                dataType: 'json',
                beforeSend : function(xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        window.alert("文章删除成功");
                        window.location.reload();
                    } else {
                        window.alert(result.msg || '文章删除失败')
                    }
                }
            });
        }
    }
    /*]]>*/
</script>
</body>
</html>